home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacPerl 5.0.3 / MacPerl Source ƒ / Perl5 / t / lib / db-recno.t < prev    next >
Encoding:
Text File  |  1995-10-30  |  3.5 KB  |  146 lines  |  [TEXT/MPS ]

  1. #!./perl
  2.  
  3. BEGIN {
  4.     chdir 't' if -d 't';
  5.     chdir '::' unless -d 'lib';
  6. #    @INC = '../lib';
  7.     @INC = '::lib';
  8.     require Config; import Config;
  9.     if ($Config{'extensions'} !~ /\bDB_File\b/) {
  10.     print "1..0\n";
  11.     exit 0;
  12.     }
  13. }
  14.  
  15. use DB_File; 
  16. use Fcntl;
  17.  
  18. print "1..30\n";
  19.  
  20. $Dfile = "Op.db-recno";
  21. unlink $Dfile;
  22.  
  23. # umask(0);
  24.  
  25. # Check the interface to RECNOINFO
  26.  
  27. $dbh = TIEHASH DB_File::RECNOINFO ;
  28. print (($dbh->{bval} == undef) ? "ok 1\n" : "not ok 1\n") ;
  29. print (($dbh->{cachesize} == undef) ? "ok 2\n" : "not ok 2\n") ;
  30. print (($dbh->{psize} == undef) ? "ok 3\n" : "not ok 3\n") ;
  31. print (($dbh->{flags} == undef) ? "ok 4\n" : "not ok 4\n") ;
  32. print (($dbh->{lorder} == undef) ? "ok 5\n" : "not ok 5\n") ;
  33. print (($dbh->{reclen} == undef) ? "ok 6\n" : "not ok 6\n") ;
  34. print (($dbh->{bfname} == undef) ? "ok 7\n" : "not ok 7\n") ;
  35.  
  36. $dbh->{bval} = 3000 ;
  37. print ($dbh->{bval} == 3000 ? "ok 8\n" : "not ok 8\n") ;
  38.  
  39. $dbh->{cachesize} = 9000 ;
  40. print ($dbh->{cachesize} == 9000 ? "ok 9\n" : "not ok 9\n") ;
  41.  
  42. $dbh->{psize} = 400 ;
  43. print (($dbh->{psize} == 400) ? "ok 10\n" : "not ok 10\n") ;
  44.  
  45. $dbh->{flags} = 65 ;
  46. print (($dbh->{flags} == 65) ? "ok 11\n" : "not ok 11\n") ;
  47.  
  48. $dbh->{lorder} = 123 ;
  49. print (($dbh->{lorder} == 123) ? "ok 12\n" : "not ok 12\n") ;
  50.  
  51. $dbh->{reclen} = 1234 ;
  52. print ($dbh->{reclen} == 1234 ? "ok 13\n" : "not ok 13\n") ;
  53.  
  54. $dbh->{bfname} = 1234 ;
  55. print ($dbh->{bfname} == 1234 ? "ok 14\n" : "not ok 14\n") ;
  56.  
  57.  
  58. # Check that an invalid entry is caught both for store & fetch
  59. eval '$dbh->{fred} = 1234' ;
  60. print ($@ eq '' ? "ok 15\n" : "not ok 15\n") ;
  61. eval '$q = $dbh->{fred}' ;
  62. print ($@ eq '' ? "ok 16\n" : "not ok 16\n") ;
  63.  
  64. # Now check the interface to RECNOINFO
  65.  
  66. print (($X = tie(@h, DB_File,$Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO )) ? "ok 17\n" : "not ok 17");
  67.  
  68. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
  69.    $blksize,$blocks) = stat($Dfile);
  70. print (($mode & 0777) == 0640 ? "ok 18\n" : "not ok 18\n");
  71.  
  72. #$l = @h ;
  73. $l = $X->length ;
  74. print (!$l ? "ok 19\n" : "not ok 19\n");
  75.  
  76. @data = qw( a b c d ever f g h  i j k longername m n o p) ;
  77.  
  78. $h[0] = shift @data ;
  79. print ($h[0] eq 'a' ? "ok 20\n" : "not ok 20\n") ;
  80.  
  81. foreach (@data)
  82.   { $h[++$i] = $_ }
  83.  
  84. unshift (@data, 'a') ;
  85.  
  86. print (defined $h[1] ? "ok 21\n" : "not ok 21\n");
  87. print (! defined $h[16] ? "ok 22\n" : "not ok 22\n");
  88. print ($X->length == @data ? "ok 23\n" : "not ok 23\n") ;
  89.  
  90.  
  91. # Overwrite an entry & check fetch it
  92. $h[3] = 'replaced' ;
  93. $data[3] = 'replaced' ;
  94. print ($h[3] eq 'replaced' ? "ok 24\n" : "not ok 24\n");
  95.  
  96. #PUSH
  97. @push_data = qw(added to the end) ;
  98. #push (@h, @push_data) ;
  99. $X->push(@push_data) ;
  100. push (@data, @push_data) ;
  101. print ($h[++$i] eq 'added' ? "ok 25\n" : "not ok 25\n");
  102.  
  103. # POP
  104. pop (@data) ;
  105. #$value = pop(@h) ;
  106. $value = $X->pop ;
  107. print ($value eq 'end' ? "not ok 26\n" : "ok 26\n");
  108.  
  109. # SHIFT
  110. #$value = shift @h
  111. $value = $X->shift ;
  112. print ($value eq shift @data ? "not ok 27\n" : "ok 27\n");
  113.  
  114. # UNSHIFT
  115.  
  116. # empty list
  117. $X->unshift ;
  118. print ($X->length == @data ? "ok 28\n" : "not ok 28\n") ;
  119.  
  120. @new_data = qw(add this to the start of the array) ;
  121. #unshift @h, @new_data ;
  122. $X->unshift (@new_data) ;
  123. unshift (@data, @new_data) ;
  124. print ($X->length == @data ? "ok 29\n" : "not ok 29\n") ;
  125.  
  126. # SPLICE
  127.  
  128. # Now both arrays should be identical
  129.  
  130. $ok = 1 ;
  131. $j = 0 ;
  132. foreach (@data)
  133. {
  134.    $ok = 0, last if $_ ne $h[$j ++] ; 
  135. }
  136. print ($ok ? "ok 30\n" : "not ok 30\n") ;
  137.  
  138. # IMPORTANT - $X must be undefined before the untie otherwise the
  139. #             underlying DB close routine will not get called.
  140. undef $X ;
  141. untie(@h);
  142.  
  143. unlink $Dfile;
  144.  
  145. exit ;
  146.